aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/[username]/invoice/[id]/+layout.server.js
blob: 6f9ae4a2230a6c986934e3dbc1c34ed3a363ffec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import Qr from "qrcode-base64";
import { get } from "$lib/utils";
import { redirect } from "@sveltejs/kit";

export async function load({ depends, params, url, parent }) {
  depends("app:invoice");

  let { user } = await parent();
  let { id } = params;
  let invoice = await get(`/invoice/${id}`);
  let options = !!url.searchParams.get("options");

  if (
    user &&
    invoice.uid !== user.id &&
    !url.pathname.includes("tip") &&
    !options
  ) {
    redirect(307, `/send/invoice/${id}`);
  }

  let { amount, pending, received } = invoice;
  amount = parseInt(amount);

  let paid =
    (!amount && (pending || received)) ||
    (amount > 0 && (pending >= amount || received >= amount));
  if (paid && !url.pathname.endsWith("paid")) {
    redirect(
      307,
      `/${params.username}/invoice/${id}/paid` +
        (options ? "?options=true" : "")
    );
  }

  let src = Qr.drawImg(invoice.text || "", { size: 340 });
  return { id, invoice, src };
}